<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%
    Integer uid = token.getUid();
    List<Map> operates = QBuilder.db("basedb").select("distinct new map (oid as expert_id)").from("OperateHistory").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("uid=", uid).and("operate=2").and("category=3")).query().getResultObj();
    if (isEmpty(operates)) {
        msg = new Message(30041, "没有关注任何医生");
        msg.put("data", new ArrayList());
        Tools.encryptPrint(out, msg);
        return;
    }
    String expertIds = "'-1'";
    List<String> list = new ArrayList<String>();
    for (int i = 0; i < operates.size(); i++) {
        Map map = operates.get(i);
        String expertId = Tools.getDesc(map.get("expert_id"), "-1");
        if (!list.contains(expertId)) {
            expertIds += ",'" + expertId + "'";
            list.add(expertId);
        }

    }
    List<Map> experts = QBuilder.db("basedb").select("new map(e.id as id, e.name as name, (case when e.expertId is NULL then '' else e.expertId end) as expert_id, (case when e.avatar is NULL then '' else e.avatar end) as avatar, (case when e.title is NULL then '' else e.title end) as title)").from("Expert e").where(new Where("e.hospital=", hospital).and("e.status = ", STATUS_ENABLE).and("e.id in("+expertIds+")")).group("expert_id").query().getResultObj();
    if (isEmpty(experts)) {
        msg = new Message(30041, "没有关注任何医生");
        msg.put("data", new ArrayList());
        Tools.encryptPrint(out, msg);
        return;
    }
    List<String> _list = new ArrayList<String>();
    for (int i = 0; i < experts.size(); i++) {
        Map map = experts.get(i);
        String expertId = Tools.getDesc(map.get("expert_id"), "-1");
        if (!_list.contains(expertId)) {
            _list.add(expertId);
        }
    }
    list.removeAll(_list);
    expertIds = "'-1'";
    for (int i = 0; i < list.size(); i++) {
        expertIds += ",'" + list.get(i) + "'";
    }
    List<Map> _experts = QBuilder.db("basedb").select("new map(s.doctorId as expert_id, s.doctorName as name, '"+ DEFAULT_AVATAR +"' as avatar)").from("Schedule as s").where(new Where("s.registerType=2").and("s.hospital=", hospital).and("s.status = ", STATUS_ENABLE).and("s.doctorId in (" + expertIds + ")")).group("s.doctorId").query().getResultObj();
    experts.addAll(_experts);
    msg.put("data", experts);
    Tools.encryptPrint(out, msg);
%>
